<?php
class HAbroadModel extends BaseModel implements Model {
    function __construct() {
        parent::__construct("habroad", "id");
        $this->addField("id", Model::TYPE_INT, Model::MOD_PRIMARY);
        $this->addField("company_id", Model::TYPE_INT, Model::MOD_FOREIGN, array("alias" => "company"));
        $this->addField("activity", Model::TYPE_TEXT);
        $this->addField("update", Model::TYPE_TEXT, Model::MOD_UPDATE);
    }

    public function getUpdateRequests() {
        $query = "SELECT habroad.id, companyname FROM habroad ".
                 "JOIN companies ON companies.company_id=habroad.company_id ".
                 "WHERE (companies.update_request is not null AND companies.update_request <> '') OR (businessleads.update IS NOT NULL AND businessleads.update <> '')"
        ;
        $res = Database::executeQuery($query);
        $result = Database::getDataFromResource($res);
        $data = array();
        for ($i = 0; $i < count($result); $i++) {
            $data[$i]['id'] = $result[$i]['businesslead_id'];
            $data[$i]['cell'] = array(
                $result[$i]['companyname']
            );
        }
        return $data;
    }

    public function getUpdateRequestCount() {
        $query = "SELECT count(*) AS c  FROM (".
                    "SELECT DISTINCT companies.company_id, companies.companyname FROM businessleads ".
                    "JOIN companies ON companies.company_id=businessleads.company_id ".
                    "WHERE accepted IS NULL OR accepted <> 1".
                 ")x"
        ;
        $res = Database::executeQuery($query);
        $data = Database::getDataFromREsource($res);
        return $data[0]['c'];
    }

    public function getLeadByCompanyId($companyId) {
        $query = "SELECT * FROM businessleads WHERE company_id=$companyId";
        $res = Database::executeQuery($query);
        $data = Database::getDataFromREsource($res);
        return $data[0];
    }
/*
    public function acceptChanges($businesslead_id) {
        $query = "UPDATE businessleads SET accepted=1 WHERE businesslead_id=$businesslead_id";
        Database::executeQuery($query);
    }

    public function rejectChanges($businesslead_id) {
        $query = "DELETE FROM businessleads WHERE businesslead_id=$businesslead_id";
        Database::executeQuery($query);
    }
*/
    public function saveUpdate($fieldset, $companyId) {
        $changes = json_encode($fieldset);
        $query = "SELECT count(*) AS c FROM businessleads WHERE company_id=$companyId";
        $res = Database::executeQuery($query);
        $data = Database::getDataFromResource($res);
        if ($data[0]['c'] == 0) {
            $query = "INSERT INTO businessleads (company_id, `update`) VALUES ($companyId, '$changes')";
            Database::executeQuery($query);
        } else {
            $query = "UPDATE businessleads SET `update`='$changes' WHERE company_id=$companyId";
            Database::executeQuery($query);
        }
    }

    public function getPreview($id) {
        $query = "SELECT * FROM businessleads ".
                 "JOIN companies ON companies.company_id=businessleads.company_id ".
                 "WHERE businesslead_id=$id"
        ;
        $res = Database::executeQuery($query);
        $data = Database::getDataFromResource($res);
        $result = $this->getPreviewData($data[0]);
        if (count($result) > 0) {
            $result['company_id'] = $data[0]['company_id'];
        } else {
            $result = $data[0];
        }
        return $result;
    }

    public function getChanges($company_id) {
        $query = "SELECT * FROM businessleads WHERE (accepted <> 1 OR accepted IS NULL) AND company_id=$company_id";
        $res = Database::executeQuery($query);
        $data = Database::getDataFromREsource($res);
        return $data;
    }

    public function renderAlias($record) {
        $result = $record;
        return $result;
    }

    public function isAuthorized($operation, $subject = "") {
        return true;
    }

    public function modifyDatabaseInput($data) {
        return $data;
    }
}

?>
